import { Injectable } from '@nestjs/common';
import { AddRoleDto } from './dto/addRoleDto';
import { log } from 'console';
import { TestRepository } from './TestRepository'
import { EditRoleDto } from './dto/editRoleDto'
import { RoleEntity } from './entity/role.entity';
import { BodyDto, restBody } from 'src/store/vo/BodyDto';

@Injectable()
export class TestService {

  constructor(private readonly testRepository: TestRepository) { }

  /**
   * 新增
   * @param addRoleDto 角色数据
   * @returns true or false
   */
  addRole(addRoleDto: AddRoleDto): Promise<BodyDto<Boolean>> {
    return this.testRepository.addRole(addRoleDto).then((res) => {
      return restBody(res.raw.length > 0)
    }).catch((error) => {
      log(error)
      return restBody(false, '新增失败', 500)
    })
  }

  /**
   * 查询
   * @param id 角色ID
   * @returns 角色信息
   */
  getRole(id: number): Promise<BodyDto<RoleEntity | null>> {
    return this.testRepository.getRole(id).then(res => {
      return restBody(res)
    }).catch((error) => {
      log(error)
      return restBody(null, '查询失败', 500)
    })
  }

  /**
   * 编辑
   * @param editRoleDto 数据信息
   * @returns true or false 
   */
  editRole(editRoleDto: EditRoleDto): Promise<BodyDto<Boolean>> {
    return this.testRepository.editRole(editRoleDto).then(res => {
      log(res)
      return restBody(res.affected ? true : false)
    }).catch((error) => {
      log(error)
      return restBody(false, '修改失败', 500)
    })
  }

  /**
   * 删除
   * @param id 角色ID
   * @returns true or false
   */
  deleteRole(id: number): Promise<BodyDto<Boolean>> {
    return this.testRepository.deleteRole(id).then(res => {
      return restBody(res.affected ? true : false)
    }).catch((error) => {
      log(error)
      return restBody(false, '删除失败', 500)
    })
  }
}
